﻿#include <QtDebug>
#include "shape_based_matching/line2Dup.h"

using namespace cv;

void test02()
{
    cv::Mat model = cv::imread("E:\\testimage\\test\\case1\\Cut.png");
    cv::Mat mask = Mat(model.size(), CV_8UC1, {255});

    line2Dup::Detector detector(256, {4, 8});
    std::string class_id = "test";
    int  templ_id = detector.addTemplate(model, class_id, mask);
    auto templ = detector.getTemplates("test", templ_id);
    qDebug() << templ.size();
    qDebug() << "templ[0].tl = " << templ[0].tl_x << ", " << templ[0].tl_y;

    auto feat = templ[0].features;
    qDebug() << "feature.size() = " << feat.size();
    for(int i = 0; i < feat.size(); i++)
    {
       if(i < 2) qDebug() << "feature = " << feat[i].x  + templ[0].tl_x << ", " << feat[i].y + templ[0].tl_y  << ", " << feat[i].label << ", " << feat[i].theta;
        cv::circle(model, { feat[i].x + templ[0].tl_x, feat[i].y + templ[0].tl_y }, 2, {0, 0, 255}, 2);
    }
    cv::imshow("model", model);
    std::vector<std::string> ids;
    ids.push_back("test");

    cv::Mat model2 = cv::imread("E:\\testimage\\test\\case1\\Cut3.png");
    auto matches = detector.match(model2, 80, ids);

    qDebug() << "matches.size() = " << matches.size();
    if(matches.size() == 0) return ;
    qDebug() << matches[0].x << ", " << matches[0].y << ", " << matches[0].similarity;


    // center x,y of train_img in test img
    float x =  matches[0].x - templ[0].tl_x + model.cols / 2.0;
    float y =  matches[0].y - templ[0].tl_y + model.rows / 2.0;

    qDebug() << "(x, y) = " << x << ", " << y;

    for(int i=0; i<templ[0].features.size(); i++)
    {
        auto feat = templ[0].features[i];
        if(i < 2) qDebug() << "feature = " << feat.x+matches[0].x << ", " << feat.y+matches[0].y;
            /// 这里差三个像素
        cv::circle(model2, {feat.x+matches[0].x /*+ templ[0].tl_x*/, feat.y+matches[0].y/*+templ[0].tl_y*/}, 3, {255, 0, 0}, -1);
    }
    cv::imshow("test", model2);
}
void test03()
{
    cv::Mat model = cv::imread("E:\\testimage\\test\\case1\\Cut.png");
    cv::Mat mask = Mat(model.size(), CV_8UC1, {255});

    line2Dup::Detector detector(12, {4, 8});
    std::string class_id = "test";
    int  templ_id = detector.addTemplate(model, class_id, mask);
    auto templ = detector.getTemplates("test", templ_id);
    qDebug() << "templ[0].tl = " << templ[0].tl_x << ", " << templ[0].tl_y;


    cv::Mat test_img = cv::imread("E:\\testimage\\test\\case1\\Cut3.png");
    int padding = 250;
    cv::Mat padded_img = cv::Mat(test_img.rows + 2*padding,
                                 test_img.cols + 2*padding, test_img.type(), cv::Scalar::all(0));
    test_img.copyTo(padded_img(Rect(padding, padding, test_img.cols, test_img.rows)));

    int stride = 16;
    int n = padded_img.rows/stride;
    int m = padded_img.cols/stride;
    Rect roi(0, 0, stride*m , stride*n);
    Mat img = padded_img(roi).clone();
    assert(img.isContinuous());

    std::vector<std::string> ids;
    ids.push_back("test");
    auto matches = detector.match(img, 1, ids);

    qDebug() << "matches.size() = " << matches.size();
    qDebug() << matches[0].x << ", " << matches[0].y << ", " << matches[0].similarity;
}

void test01()
{
    cv::Mat model = cv::imread("E:\\testimage\\test\\case1\\Cut2.png");
    cv::Mat mask = Mat(model.size(), CV_8UC1, {255});

    line2Dup::Detector detector(4, {4, 8});
    std::string class_id = "test";
    int  templ_id = detector.addTemplate(model, class_id, mask);
    auto templ = detector.getTemplates("test", templ_id);

    qDebug() << "templ.size() = " <<  templ.size();
    qDebug() << "templ[0].pyramid_level = " <<  templ[0].pyramid_level;
    qDebug() << "templ[1].pyramid_level = " <<  templ[1].pyramid_level;

    qDebug() << "templ[0].width height = " << templ[0].width << ", " << templ[0].height;
    qDebug() << "templ[0].tl = " << templ[0].tl_x << ", " << templ[0].tl_y;

    auto feat = templ[0].features;
    qDebug() << "feature.size() = " << feat.size();
    for(int i = 0; i < feat.size(); i++)
    {
        qDebug() << "feature = " << feat[i].x << ", " << feat[i].y << ", " << feat[i].label << ", " << feat[i].theta;
        cv::circle(model, { feat[i].x + templ[0].tl_x, feat[i].y + templ[0].tl_y }, 2, {0, 0, 255}, 2);
    }
    cv::imshow("model", model);

    model = cv::imread("E:\\testimage\\test\\case1\\Cut3.png");
    mask = Mat(model.size(), CV_8UC1, {255});

    line2Dup::Detector detector2(4, {4, 8});
    templ_id = detector2.addTemplate(model, class_id, mask);
    templ = detector2.getTemplates("test", templ_id);

    qDebug() << "templ[0].width height = " << templ[0].width << ", " << templ[0].height;
    qDebug() << "templ[0].tl = " << templ[0].tl_x << ", " << templ[0].tl_y;

    feat = templ[0].features;
    qDebug() << "feature.size() = " << feat.size();
    for(int i = 0; i < feat.size(); i++)
    {
        qDebug() << "feature = " << feat[i].x << ", " << feat[i].y << ", " << feat[i].label << ", " << feat[i].theta;
        cv::circle(model, { feat[i].x + templ[0].tl_x, feat[i].y + templ[0].tl_y }, 2, {0, 0, 255}, 2);
    }
    cv::imshow("model2", model);
}
